﻿Public Class tema_x_cd
    Enum estado
        inicial
        en_uso
    End Enum
    Dim operacion As estado = estado.inicial
    Dim id As String = ""
    Dim tratamiento As New Tratamiento_Objetos
    Dim datos As New datos
    Dim temas_inicio As New Data.DataTable
    Dim todos_temas As New Data.DataTable
    Dim nombre As String = "autor"
    Dim pk As String = "id_autor"
    Dim nombre_tabla As String = "autor"

    Dim tamaño_temas_que_interpreta As Integer = 0
    Dim tamaño_todos_temas As Integer = 0


    Private Sub botones(ByVal p1 As Boolean)
        btn_agregar.Enabled = p1
        btn_cancelar.Enabled = p1
        btn_guardar.Enabled = p1
        btn_cargar.Enabled = Not p1
        btn_quitar.Enabled = p1
        btn_quitar_todo.Enabled = p1
        btn_salir.Enabled = p1
    End Sub


    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.KeyChar = ""
        End If
    End Sub
    Private Function validar() As Boolean
        If String.IsNullOrEmpty(TextBox1.Text) Then
            MessageBox.Show("Ingrese el codigo del CD", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TextBox1.Focus()
            lbl_cod_cd.ForeColor = Color.Red
            Return False
        Else
            lbl_cod_cd.ForeColor = Color.Black
            Return True
        End If
    End Function
    Private Sub btn_cargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cargar.Click
        If validar() = True Then
            Dim data As New Data.DataTable
            data = datos.leo_tabla("cd", "codigo_cd = '" & TextBox1.Text & "'")
            If data.Rows.Count > 0 Then
                lbl_nombre_cd.Text = data.Rows(0)(1)
                botones(True)
                TextBox1.Enabled = False
                Dim sql As String = "SELECT temasxcd.codigo_tema, tema.titulo, temasxcd.id_autor, autor.nombre, autor.apellido FROM (temasxcd INNER JOIN tema ON temasxcd.codigo_tema = tema.codigo_tema INNER JOIN autor ON temasxcd.id_autor = autor.id_autor) where codigo_cd = '" & TextBox1.Text & "'"
                temas_inicio = datos.ejecutar_sql(sql)

                Dim fila As Integer = 0

                DataGridView1.Rows.Clear()
                tamaño_temas_que_interpreta = temas_inicio.Rows.Count
                If tamaño_temas_que_interpreta > 0 Then
                    For fila = 0 To temas_inicio.Rows.Count - 1
                        DataGridView1.Rows.Add()
                        DataGridView1.Rows(fila).Cells(0).Value = temas_inicio.Rows(fila)(0)
                        DataGridView1.Rows(fila).Cells(1).Value = temas_inicio.Rows(fila)(1)
                        DataGridView1.Rows(fila).Cells(2).Value = temas_inicio.Rows(fila)(2)
                        DataGridView1.Rows(fila).Cells(3).Value = temas_inicio.Rows(fila)(3)
                        DataGridView1.Rows(fila).Cells(4).Value = temas_inicio.Rows(fila)(4)
                    Next
                End If


                todos_temas = datos.ejecutar_sql("SELECT temasxinterprete.codigo_tema, tema.titulo, temasxinterprete.id_autor, autor.nombre, autor.apellido FROM temasxinterprete INNER JOIN tema ON temasxinterprete.codigo_tema = tema.codigo_tema INNER JOIN autor ON temasxinterprete.id_autor = autor.id_autor order by nombre")

                DataGridView2.Rows.Clear()
                tamaño_todos_temas = todos_temas.Rows.Count
                If tamaño_todos_temas > 0 Then
                    For fila = 0 To todos_temas.Rows.Count - 1
                        DataGridView2.Rows.Add()
                        DataGridView2.Rows(fila).Cells(0).Value = todos_temas.Rows(fila)(0)
                        DataGridView2.Rows(fila).Cells(1).Value = todos_temas.Rows(fila)(1)
                        DataGridView2.Rows(fila).Cells(2).Value = todos_temas.Rows(fila)(2)
                        DataGridView2.Rows(fila).Cells(3).Value = todos_temas.Rows(fila)(3)
                        DataGridView2.Rows(fila).Cells(4).Value = todos_temas.Rows(fila)(4)
                    Next
                End If

                DataGridView1.ClearSelection()
                DataGridView2.ClearSelection()
                btn_agregar.Enabled = False
                btn_guardar.Enabled = False
                btn_quitar.Enabled = False
                btn_nuevo.Enabled = False

            Else
                MessageBox.Show("No existe el cd ingresado", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox1.Focus()
            End If
        End If
    End Sub

    Private Sub btn_agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_agregar.Click
        'MessageBox.Show(DataGridView2.CurrentRow.Index & " " & tamaño_todos_temas)
        If DataGridView2.CurrentRow.Index = (tamaño_todos_temas) Then
            MessageBox.Show("Seleccione un tema", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Dim flag As Boolean = True
            For fila = 0 To tamaño_temas_que_interpreta - 1
                If DataGridView1.Rows(fila).Cells(0).Value = DataGridView2.CurrentRow.Cells(0).Value And DataGridView1.Rows(fila).Cells(2).Value = DataGridView2.CurrentRow.Cells(2).Value Then
                    flag = False
                    Exit For
                End If
            Next
            If flag = False Then
                MessageBox.Show("Este tema ya esta ingresado", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                DataGridView1.Rows.Add()
                DataGridView1.Rows(tamaño_temas_que_interpreta).Cells(0).Value = DataGridView2.CurrentRow.Cells(0).Value
                DataGridView1.Rows(tamaño_temas_que_interpreta).Cells(1).Value = DataGridView2.CurrentRow.Cells(1).Value
                DataGridView1.Rows(tamaño_temas_que_interpreta).Cells(2).Value = DataGridView2.CurrentRow.Cells(2).Value
                DataGridView1.Rows(tamaño_temas_que_interpreta).Cells(3).Value = DataGridView2.CurrentRow.Cells(3).Value
                DataGridView1.Rows(tamaño_temas_que_interpreta).Cells(4).Value = DataGridView2.CurrentRow.Cells(4).Value

                tamaño_temas_que_interpreta = tamaño_temas_que_interpreta + 1
                DataGridView2.ClearSelection()
                DataGridView1.CurrentCell = DataGridView1.Rows(tamaño_temas_que_interpreta - 1).Cells(0)
                btn_agregar.Enabled = False
                btn_guardar.Enabled = True
                btn_cancelar.Enabled = True
                operacion = estado.en_uso
                btn_nuevo.Enabled = False
            End If
        End If
    End Sub

    Private Sub btn_quitar_todo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_quitar_todo.Click
        DataGridView1.Rows.Clear()
        tamaño_temas_que_interpreta = 0
        btn_guardar.Enabled = True
        operacion = estado.en_uso
        btn_nuevo.Enabled = False
    End Sub

    Private Sub btn_quitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_quitar.Click
        If DataGridView1.CurrentRow.Index = (tamaño_temas_que_interpreta) Then
            MessageBox.Show("Seleccione un tema", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
            DataGridView1.ClearSelection()
            tamaño_temas_que_interpreta = tamaño_temas_que_interpreta - 1
            btn_quitar.Enabled = False
            btn_guardar.Enabled = True
            btn_cancelar.Enabled = True
            operacion = estado.en_uso
            btn_nuevo.Enabled = False
        End If
    End Sub

    Private Sub btn_cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancelar.Click
        DataGridView1.Rows.Clear()
        Dim sql As String = "SELECT temasxcd.codigo_tema, tema.titulo, temasxcd.id_autor, autor.nombre, autor.apellido FROM (temasxcd INNER JOIN tema ON temasxcd.codigo_tema = tema.codigo_tema INNER JOIN autor ON temasxcd.id_autor = autor.id_autor) where codigo_cd = '" & TextBox1.Text & "'"
        temas_inicio = datos.ejecutar_sql(sql)
        tamaño_temas_que_interpreta = temas_inicio.Rows.Count
        If tamaño_temas_que_interpreta > 0 Then
            For fila = 0 To temas_inicio.Rows.Count - 1
                DataGridView1.Rows.Add()
                DataGridView1.Rows(fila).Cells(0).Value = temas_inicio.Rows(fila)(0)
                DataGridView1.Rows(fila).Cells(1).Value = temas_inicio.Rows(fila)(1)
                DataGridView1.Rows(fila).Cells(2).Value = temas_inicio.Rows(fila)(2)
                DataGridView1.Rows(fila).Cells(3).Value = temas_inicio.Rows(fila)(3)
                DataGridView1.Rows(fila).Cells(4).Value = temas_inicio.Rows(fila)(4)
            Next
        End If
        btn_guardar.Enabled = False
        btn_nuevo.Enabled = True
        operacion = estado.inicial
    End Sub

    Private Sub btn_guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_guardar.Click
        For indice = 0 To temas_inicio.Rows.Count - 1
            Dim flag As Boolean = False
            For indice2 = 0 To tamaño_temas_que_interpreta - 1
                If Integer.Parse(temas_inicio.Rows(indice)(0)) = Integer.Parse(DataGridView1.Rows(indice2).Cells(0).Value) And Integer.Parse(temas_inicio.Rows(indice)(2)) = Integer.Parse(DataGridView1.Rows(indice2).Cells(2).Value) Then
                    flag = True
                    Exit For
                End If
            Next
            'si termino el bucle sin break eliminar el actual
            If flag = False Then
                'eliminar este tema
                Dim sql As String = "delete from temasxcd where codigo_cd = '" & TextBox1.Text & "' and codigo_tema = '" & temas_inicio.Rows(indice)(0) & "' and id_autor = '" & temas_inicio.Rows(indice)(2) & "'"

                datos.ejecutar_sql(sql)
            End If
        Next


        For indice = 0 To tamaño_temas_que_interpreta - 1
            Dim flag As Boolean = False
            For indice2 = 0 To temas_inicio.Rows.Count - 1
                If Integer.Parse(temas_inicio.Rows(indice2)(0)) = Integer.Parse(DataGridView1.Rows(indice).Cells(0).Value) And Integer.Parse(temas_inicio.Rows(indice2)(2)) = Integer.Parse(DataGridView1.Rows(indice).Cells(2).Value) Then
                    flag = True
                    Exit For
                End If
            Next
            'si termino el bucle sin break agregar el actual
            If flag = False Then
                'insert into este tema
                Dim sql As String = "insert into temasxcd(codigo_tema,id_autor,codigo_cd) values ('" & DataGridView1.Rows(indice).Cells(0).Value & "','" & DataGridView1.Rows(indice).Cells(2).Value & "','" & TextBox1.Text & "')"
                datos.ejecutar_sql(sql)
            End If
        Next
        btn_guardar.Enabled = False
        btn_cancelar.Enabled = False
        operacion = estado.inicial
        btn_nuevo.Enabled = True
        MessageBox.Show("Cambios realizados con exito", "Informacion", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        btn_agregar.Enabled = True
        btn_agregar.Focus()
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        btn_quitar.Enabled = True
        btn_quitar.Focus()
    End Sub

    Private Sub temas_x_interpretes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        botones(False)
        btn_salir.Enabled = True
        btn_nuevo.Enabled = False
    End Sub

    Private Sub temas_x_interpretes_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If operacion = estado.inicial Then
            If MessageBox.Show("¿Desea salir?", "Alerta", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        Else
            MessageBox.Show("Termine la operacion actual!", "Notificacion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            e.Cancel = True
        End If
    End Sub

    Private Sub btn_salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_salir.Click
        Me.Close()
    End Sub

    Private Sub btn_nuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nuevo.Click
        botones(False)
        TextBox1.Enabled = True
        TextBox1.Text = ""
        btn_nuevo.Enabled = False
        btn_salir.Enabled = True
        lbl_nombre_cd.Text = ""
        DataGridView1.Rows.Clear()
        DataGridView2.Rows.Clear()
    End Sub

End Class